package com.client.view;

import com.client.controller.ClientSocketThreadManager;
import com.client.model.FirstConnector;
import com.client.controller.FriendListFrameManager;
import com.common.Message;
import com.common.MessageType;
import com.common.User;
import com.server.view.RegisterFrame;
import com.util.ImageScale;
import com.util.WindowXY;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectOutputStream;


/**新的登录界面：
 * @updater: lyh 2021/9/28
 * 使用新的登录页面原登录页面迁徙到现有的登录页
 *
 */
public class LonginFrame2 extends JFrame{
    private JPanel contentPanel=new JPanel();
    private JTextField id;
    private JPasswordField pwd;
    private JButton login_btn;
    private JButton register_btn;

    private static final int DIALOG_WIDTH=414;
    private static final int DIALOG_HEIGHT=340;
    private static final int DIALOG_HEIGHT_EXTEND=573;

    public static void main(String[] args) {
        try {
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        }catch (Exception e){
            System.out.println("加载炫彩皮肤失败");
        }
        new LonginFrame2();
    }

    //初始化登录页面
    public LonginFrame2(){
        JFrame frame=new JFrame();
        setTitle("登录");
        setAlwaysOnTop(true);
        setResizable(false);
        setBounds(400, 100, DIALOG_WIDTH,DIALOG_HEIGHT);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);

        //设置居中
        setLocation(WindowXY.getXY(LonginFrame2.this.getSize()));

        //按钮文本设置
        //注册:
        register_btn=new JButton("注册");
        register_btn.setBounds(53,224,93,23);
        contentPanel.add(register_btn);


        //登录
        login_btn=new JButton("登录");
        login_btn.setBounds(190,224,93,23);
        contentPanel.add(login_btn);

        //账号
        id=new JTextField();
        id.setBounds(133, 147, 150, 25);
        contentPanel.add(id);

        //密码
        pwd=new JPasswordField();
        pwd.setBounds(133, 182, 150, 25);
        contentPanel.add(pwd);

        JLabel lblNewLabel = new JLabel("账 号");
        lblNewLabel.setBounds(53, 151, 54, 15);
        contentPanel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("密 码");
        lblNewLabel_1.setBounds(53, 194, 54, 15);
        contentPanel.add(lblNewLabel_1);


        //登录图片展示
        JLabel lblNewLabel_2 = new JLabel("New label");
        lblNewLabel_2.setBounds(0, 0, 360, 136);
        ImageIcon icon=new ImageIcon("images/timg.jpg");
        icon= ImageScale.getImage(icon, lblNewLabel_2.getWidth(), lblNewLabel_2.getHeight());
        lblNewLabel_2.setIcon((icon));
        contentPanel.add(lblNewLabel_2);

        //注册事件
        register_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
                new RegisterFrame();
            }
        });

        login_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                doLogin();
            }
        });

        //注册页面展开

        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }
    private void doLogin(){

        User u = new User();
        u.setId(id.getText().trim());
        u.setPwd(new String(pwd.getPassword()));
        if(new FirstConnector().sendLoginInfoToServer(u)){//checkUser已经启动线程
            try{
                //把创建好友列表的语句提前
                FriendListFrame friendListFrame = new FriendListFrame(this,u.getId());
                FriendListFrameManager.addQqFriendList(u.getId(), friendListFrame);

                //发送要求返回在线好友的请求包
                ObjectOutputStream oos = new ObjectOutputStream
                        (ClientSocketThreadManager.getClientConServerThread(u.getId()).getS().getOutputStream());
                //做一个包
                Message message = new Message();
                message.setMesType(MessageType.message_get_onLineFriend);
                //指明谁来要好友
                message.setSender(u.getId());
                oos.writeObject(message);

            }catch (Exception arg0){
                arg0.printStackTrace();
            }
            //关闭登录页面
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(this,"用户名或密码错误,或者帐号已登录,或者服务器正在维护");
        }
    }
}
